//
//  MLWTipsICookOtherCell.m
//  KITCHEN_STORIES
//
//  Created by 孟令通 on 16/6/14.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWTipsICookOtherCell.h"

#import "MLWTipsICookOtherModel.h"

#import "MLWTipsICookOtherSuitableModel.h"

#import "MLWTipsICookOtherTableViewCell.h"

#import "MLWTipsICookOtherHeaderView.h"

#import "MLWBaseView.h"

@implementation MLWTipsICookOtherCell

-(id)initWithIdentifier:(NSString *)identifier
{
    self = [super initWithIdentifier:identifier];
    
    if (self) {
        
        self.otherArray = [NSArray array];
        self.identifier = identifier;
    }
    
    return self;
}

-(void)setOtherArray:(NSArray *)otherArray
{
    if (_otherArray != otherArray) {
        
        [_otherArray release];
        
        _otherArray = [otherArray retain];
        
        MLWTipsICookOtherModel *model = [self.otherArray firstObject];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 300)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        [self addSubview:imageView];
        [imageView release];
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 310, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
        
        tableView.scrollEnabled = NO;
        
        tableView.delegate = self;
        
        tableView.dataSource = self;
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [self addSubview:tableView];
        [tableView release];
        
        [tableView registerClass:[MLWTipsICookOtherTableViewCell class] forCellReuseIdentifier:@"cell"];
        
        [tableView registerClass:[MLWTipsICookOtherHeaderView class] forHeaderFooterViewReuseIdentifier:@"header"];
        
    }
}


#pragma mark -
#pragma mrak  <UITableViewDelegate, UITableViewDataSource>

#pragma mark 返回分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    MLWTipsICookOtherModel *model = [self.otherArray firstObject];
    
    if (section == 0) {
        
        return model.withModelArray.count;
        
    }
    
    if (section == 1) {
        
        return model.notWithModelArray.count;
        
    }
    
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MLWTipsICookOtherModel *model = [self.otherArray firstObject];
    
    if (indexPath.section == 0) {
        
        MLWTipsICookOtherSuitableModel *withModel = [model.withModelArray objectAtIndex:indexPath.row];
        
        MLWTipsICookOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        cell.model = withModel;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
    if (indexPath.section == 1) {
        
        MLWTipsICookOtherSuitableModel *notWithModel = [model.notWithModelArray objectAtIndex:indexPath.row];
        
        MLWTipsICookOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        cell.model = notWithModel;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
    
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MLWTipsICookOtherModel *model = [self.otherArray firstObject];
    
    MLWTipsICookOtherHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    
    header.section = section;
    
    header.model = model;
    
    return header;
}





@end
